#################################################################################### 
####################################################################################
####################################################################################
# Appendix G: Tables G.20 - G.23, M.53-M.56
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
####################################################################################
####################################################################################
## Read in survey data (from: 8_2020_analysis.R)
dat = readRDS(here("data/dat_unrestricted.rds"))
datb = readRDS(here("data/dat_restricted.rds"))

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Bushenyi","Ibanda","Jinja","Mbale","Mpigi","Pallisa","Mbarara","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
dat[, paste0("c_", cols)] = lapply(dat[, cols], function(x) (x - mean(x))  )
covariates = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment",
               "c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment")

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Ibanda","Pallisa","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
datb[, paste0("c_", cols)] = lapply(datb[, cols], function(x) (x - mean(x))  )
covariatesb = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment", 
                "c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment")


####################################################################################
## Policy Priorities ####

# Unrestricted
for (i in names(dat[c("priority_health_dis_st", "priority_health_nat_st", "priority_health_index")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("priority_health_dis_st", "priority_health_nat_st", "priority_health_index")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table K.33

stargazer(m.priority_health_index.t, bm.priority_health_index.t, m.priority_health_dis_st.t, 
          bm.priority_health_dis_st.t,m.priority_health_nat_st.t, bm.priority_health_nat_st.t,  
          
          se = list(c.priority_health_index.t[,2], bc.priority_health_index.t[,2], c.priority_health_dis_st.t[,2], 
                    bc.priority_health_dis_st.t[,2],c.priority_health_nat_st.t[,2], bc.priority_health_nat_st.t[,2] ),
          
          p = list(c.priority_health_index.t[,4], bc.priority_health_index.t[,4], c.priority_health_dis_st.t[,4], 
                   bc.priority_health_dis_st.t[,4],c.priority_health_nat_st.t[,4], bc.priority_health_nat_st.t[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/priority_1_c.tex",
          label = "tab:priority_1_c",column.sep.width = "1pt", table.placement = "!ht",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Prioritization of Health for Government to Address",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", table.placement = "!ht",notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt Index","District","National"), column.separate = c(2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes")))


####################################################################################
####################################################################################
## Table K.33

stargazer(m.priority_health_index.c, bm.priority_health_index.c, m.priority_health_dis_st.c, 
          bm.priority_health_dis_st.c,m.priority_health_nat_st.c, bm.priority_health_nat_st.c,  
          
          se = list(c.priority_health_index.c[,2], bc.priority_health_index.c[,2], c.priority_health_dis_st.c[,2], 
                    bc.priority_health_dis_st.c[,2],c.priority_health_nat_st.c[,2], bc.priority_health_nat_st.c[,2] ),
          
          p = list(c.priority_health_index.c[,4], bc.priority_health_index.c[,4], c.priority_health_dis_st.c[,4], 
                   bc.priority_health_dis_st.c[,4],c.priority_health_nat_st.c[,4], bc.priority_health_nat_st.c[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/priority_1_nc.tex",
          label = "tab:priority_1_nc",column.sep.width = "1pt", table.placement = "!ht",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Prioritization of Health for Government to Address (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", table.placement = "!ht",notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt Index","District","National"), column.separate = c(2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes")))

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))
